import Cookies from 'js-cookie'

export default {
  install(Vue) {
    if (window.localStorage) {
      let s = window.localStorage;
      Vue['localStorageGet'] = (key) => {
        return JSON.parse(s.getItem(key))
      };
      Vue['localStorageSet'] = (key, value) => {
        return s.setItem(key, JSON.stringify(value))
      };
      Vue['localStorageRemove'] = (key) => {
        return s.removeItem(key)
      };
      Vue['localStorageClear'] = () => {
        return s.clear()
      };
      Vue.prototype.$localStorageGet = (key) => {
        return Vue.localStorageGet(key)
      };
      Vue.prototype.$localStorageSet = (key, value) => {
        return Vue.localStorageSet(key, value)
      };
      Vue.prototype.$localStorageRemove = (key) => {
        return Vue.localStorageRemove(key)
      };
      Vue.prototype.$localStorageClear = () => {
        return Vue.localStorageClear()
      };
    }
    if (window.sessionStorage) {
      let s = window.sessionStorage;
      Vue['sessionStorageGet'] = (key) => {
        return JSON.parse(s.getItem(key))
      };
      Vue['sessionStorageSet'] = (key, value) => {
        return s.setItem(key, JSON.stringify(value))
      };
      Vue['sessionStorageRemove'] = (key) => {
        return s.removeItem(key)
      };
      Vue['sessionStorageClear'] = () => {
        return s.clear()
      };
      Vue.prototype.$sessionStorageGet = (key) => {
        return Vue.sessionStorageGet(key)
      };
      Vue.prototype.$sessionStorageSet = (key, value) => {
        return Vue.sessionStorageSet(key, value)
      };
      Vue.prototype.$sessionStorageRemove = (key) => {
        return Vue.sessionStorageRemove(key)
      };
      Vue.prototype.$sessionStorageClear = () => {
        return Vue.sessionStorageClear()
      };
    }
    if (Cookies) {
      Vue.cookiesGet = (key, options) => {
        return JSON.parse(Cookies.get(key, options))
      };
      Vue.cookiesSet = (key, value, options) => {
        Cookies.set(key, JSON.stringify(value), options)
      };
      Vue.cookiesRemove = (key, options) => {
        Cookies.remove(key, options)
      };
      Vue.cookiesGetAll = () => {
        return JSON.parse(Cookies.getJSON())
      };
      Vue.prototype.$cookiesGet = (key, options) => {
        return Vue.cookiesGet(key, options)
      };
      Vue.prototype.$cookiesSet = (key, value, options) => {
        Vue.cookiesSet(key, value, options)
      };
      Vue.prototype.$cookiesRemove = (key, options) => {
        Vue.cookiesRemove(key, options)
      };
      Vue.prototype.$cookiesGetAll = () => {
        return Vue.cookiesGetAll()
      }
    }
  }
}

/**
 * 绑定存储接口到Vue(TODO IDEA提示效果不好，所以暂时不使用)
 * @param storage
 * @param Vue
 */
function bind(storage, Vue) {
  let s = window[storage];
  let get = storage + 'Get';
  let set = storage + 'Set';
  let remove = storage + 'Remove';
  let clear = storage + 'Clear';

  Vue[get] = (key) => {
    return JSON.parse(s.getItem(key))
  };
  Vue[set] = (key, value) => {
    s.setItem(key, JSON.stringify(value))
  };
  Vue[remove] = (key) => {
    s.removeItem(key)
  };
  Vue[clear] = () => {
    s.clear()
  };
  Object.defineProperties(Vue.prototype, {
    ['$' + get]: {
      value: (key) => {
        return Vue[get](key)
      }
    },
    ['$' + set]: {
      value: (key, value) => {
        Vue[set](key, value)
      }
    },
    ['$' + remove]: {
      value: (key) => {
        Vue[remove](key)
      }
    },
    ['$' + clear]: {
      value: () => {
        Vue[clear]()
      }
    }
  })
}
